var rekind;

$(function () {
    //初始化充值界面查询表格
    $("#member_datagrid").datagrid({
        url: "/member/list",
        fit: true,
        fitColumns: true,
        pagination: true,
        striped: true,
        singleSelect: true,
        pageList: [9],
        pageSize: 9,
        columns: [[
            {field: "id", title: "编号", hidden: true},
            {field: "cardno", title: "会员卡号", width: 100},
            {field: "name", title: "会员姓名", width: 100},
            {field: "tel", title: "电话", width: 100},
            {field: "level", title: "等级", width: 100, formatter: function (value, row, index) {
                return value.levelName;
            }},
            {field: "balance", title: "卡内余额", width: 100}
        ]],
        //绑定行选中事件,将左侧会员信息,填充到右侧
        onClickRow: function (index, row) {
            var row = $("#member_datagrid").datagrid("getSelected");
            $("#member_form").form("load", row);
            console.log(row.level.levelName);
            $("#levelName").textbox("setValue",row.level.levelName);
            $("#memberId").val(row.id);
            $("#amountbefore").val(row.balance);


        }
    })

    //初始化充值界面充值记录表格
    $("#rechargebill_datagrid").datagrid({
        url: "/rechargeBill/list",
        fit: true,
        fitColumns: true,
        pagination: true,
        striped: true,
        singleSelect: true,
        pageList: [9],
        pageSize: 9,
        columns: [[
            {
                field: "cardno", title: "会员卡号", width: 60, formatter: function (value, row, index) {
                return row.member.cardno;
            }
            },
            {field: "rekind", title: "操作方式", width: 60, formatter: function (value, row, index) {
                if(value==1){
                    return "充值";
                }else if(value==2){
                    return "扣费";
                }
            }},
            {field: "paykind", title: "支付方式", width: 60, formatter: function (value, row, index) {
                if(value==1){
                    return "现金";
                }else if(value==2){
                    return "储蓄卡";
                }else if(value==3){
                    return "支付宝";
                }else if(value==4){
                    return "微信";
                }
            }},
            {field: "amountbefore", title: "变动前金额", width: 60},
            {field: "reamount", title: "充值金额", width: 60},
            {field: "giftamount", title: "赠送金额", width: 60},
            {field: "amountafter", title: "变动后余额", width: 60},
            {field: "time", title: "充值日期", width: 100},
            {field: "remark", title: "备注", width: 60}
        ]]
    });

    //初始化搜索框
    $('#ss').searchbox({
        searcher: function (value) {
            $("#member_datagrid").datagrid("load", {
                keyword: value
            });
        },
        prompt: '输入姓名、手机号或卡号'
    });

    //为rekind单选框绑定点击事件
    $(":radio").click(function () {
        var name = $(this).prop("name");
        var value = $(this).val();
        if (name == "rekind") {
            rekind = value;
        }
        var reamount = $("#reamount").val();
        var giftamount = $("#giftamount").val();
        var totalAmount = reamount * rekind + giftamount * 1;
        $("#totalAmount").text("¥" + totalAmount);
    });

    //充值金额值改变事件,计算合计
    //jquery对象不能直接相加,草,   Number(xx)转换或者  *1 得到
    $("#reamount").bind("blur", function () {
        var reamount = $("#reamount").val();
        var giftamount = $("#giftamount").val();
        var totalAmount = reamount * rekind + giftamount * 1;
        $("#totalAmount").text("¥" + totalAmount);

    });

})


//充值的按钮方法
function recharge() {
    console.log(rekind);
    var name = $("#name").textbox("getValue");
    var totalAmount = $("#totalAmount").text();
    var str = "充值";
    if (rekind != 1) {
        str = "扣除";
    }
    $.messager.confirm('确认', '您确认要为会员[ ' + name + ' ]' + str + totalAmount + ' 吗？', function (r) {
        if (r) {
            $("#recharge_form").form('submit', {
                url: '/rechargeBill/save',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert("温馨提示", data.errorMsg);
                    } else {
                        $.messager.alert("温馨提示", "操作成功!");
                    }
                    //刷新两个表格
                    $("#member_form").form("clear");
                    $("#recharge_form").form("clear");
                    $("#member_datagrid").datagrid("reload");
                    $("#rechargebill_datagrid").datagrid("reload");
                }
            });
        }
    });
}
